import React, { useRef } from 'react'
import { Layout } from 'antd';
import MyHeader from './components/MyHeader';
import MySider from './components/MySider';
import MyContent from './components/MyContent';
import api from "../../apis/user"
import eventBus from '../../utils/eventBus';

export default function NavPage() {
    const token = localStorage.token
    const renderCount = useRef(0)
    const [isTrueToken, setIsTrueToken] = React.useState(false);//假设token是假的
    React.useEffect(()=>{
        eventBus.emit("refreshRoutes")
    },[])
    if (!token) {
        window.location.assign("/login")
        return null
    } else {
        if (renderCount.current == 0) {
            api.getUserInfo()
                .then(() => {
                    //如果代码执行到这里，说明token是真的
                    setIsTrueToken(true)
                })
                .catch(err => {
                    console.log(err);
                    window.location.assign("/login")
                })
            renderCount.current++
        }
    }
    if (isTrueToken) {
        return (
            <Layout style={{ height: "100vh" }}>
                <MyHeader />
                <Layout>
                    <MySider></MySider>
                    <MyContent></MyContent>
                </Layout>
            </Layout>
        )
    } else {
        return null
    }
}
